<?php


namespace App\Transforms;


use App\Enum\CarEnum;
use App\Models\DriverCar;
use App\Models\Drivers;
use App\Models\UploadFile;
use BobCoder\Construct\Foundation\Transform\TransformContract;

class CarTransformer implements TransformContract
{

    public function transform($attributes)
    {
        if (!$attributes) return $attributes;

        if ($attributes->license_driving) {
            if(!is_array($attributes->license_driving)) $attributes->license_driving = json_decode($attributes->license_driving,true);
            $attributes->license_driving = (new UploadFile())
                ->select(['id', 'file_url', 'file_name'])
                ->whereIn('id', $attributes->license_driving)
                ->get()
                ->toArray();
        }

        if ($attributes->driver){
             foreach ($attributes->driver as &$item){
                 $item['host'] = DriverCar::where(['car_id' => $attributes->id, 'driver_id'=> $item->id])->value('host');
             }
        }

        return $attributes;
    }


    /**
     * 格式化数据
     * @param $list
     * @return mixed
     * @Author 姿势就是力量
     */
    public function transformList($list)
    {
        if (!$list) return $list;

        foreach ($list as &$item) {
            $item['ownership'] = $item['ownership'] == CarEnum::PERSON ? '个人' : '公司';
            $item['company'] = $item['company']['name'];
            if (isset($item['brand']) && $item['brand']) $item['brand'] = $item['brand']['name'];
            if (isset($item['driver']) && $item['driver']) {
                $car_driver_group = array_column($item['driver'], 'real_name','host');
                $host = array_column($item['driver'], 'host');
                $item['host']=trim($host[0]) == '1'? '是': '否';
                $item['driver_main'] = isset($car_driver_group[1])?$car_driver_group[1]:'';
                $item['driver'] = implode(',', array_column($item['driver'], 'real_name'));
            }
            if (isset($item['service']) && $item['service']) {
                $item['service'] = $item['service']['name'];
            }
        }

        return $list;
    }

    /**
     * 格式化公共接口car数据
     * @param $list
     * @return mixed
     * @Author 姿势就是力量
     */
    public function transformgetcarList($list)
    {
        if (!$list) return $list;

        foreach ($list as &$item) {
            $item['ownership'] = $item['ownership'] == CarEnum::PERSON ? '个人' : '公司';
            $item['company'] = $item['company']['name'];
            if (isset($item['brand']) && $item['brand']) $item['brand'] = $item['brand']['name'];
            if (isset($item['driver']) && $item['driver']) {
                $car_driver_group = array_column($item['driver'], 'real_name','host');
                $host = array_column($item['driver'], 'host');
                $item['host']=trim($host[0]) == '1'? '是': '否';
                $item['driver_main'] = isset($car_driver_group[1])?$car_driver_group[1]:'';
                $item['driver'] = implode(',', array_column($item['driver'], 'real_name'));
            }
            if (isset($item['service']) && $item['service']) {
                $item['type'] = $item['service']['name'];
            }
            unset($item['service']);
        }

        return $list;
    }
}
